/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pkl51.service.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import pkl51.database.DatabaseUtility;
import pkl51.entity.Blok7;
import pkl51.error.EntityException;
import pkl51.service.Blok7Dao;

/**
 *
 * @author toshiba
 */
public class Blok7DaoImpl implements Blok7Dao{
    
    private Connection connection;
    private final String insertBlok7 = "INSERT INTO blok7 VALUES (?, ?) ON DUPLICATE KEY UPDATE catatan = ?";
    private final String updateBlok7 = "UPDATE blok7 SET catatan = ? WHERE NKS = ?";
    private final String getBlok7ByNKS = "SELECT * FROM blok7 WHERE NKS = ?";
    
    @Override
    public void insertBlok7(Blok7 blok7) throws EntityException {
        PreparedStatement statement = null;
        try {
            connection = DatabaseUtility.getConnection();
            connection.setAutoCommit(false);
            statement = connection.prepareStatement(insertBlok7);

            statement.setString(1, blok7.getNKS());
            statement.setString(2, blok7.getCatatan());
            statement.setString(3, blok7.getCatatan());
            statement.executeUpdate();

            connection.commit();

        } catch (SQLException ex) {
            try {
                connection.rollback();
            } catch (SQLException ex1) {
                Logger.getLogger(Blok7DaoImpl.class.getName()).log(Level.SEVERE, null, ex1);
            }
            Logger.getLogger(Blok7DaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                connection.setAutoCommit(true);
            } catch (SQLException ex) {
                Logger.getLogger(Blok7DaoImpl.class.getName()).log(Level.SEVERE, null, ex);
            }
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(Blok7DaoImpl.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    @Override
    public void updateBlok7(Blok7 blok7) throws EntityException {
        PreparedStatement statement = null;
        try {
            connection = DatabaseUtility.getConnection();
            connection.setAutoCommit(false);
            statement = connection.prepareStatement(updateBlok7); //isikan statement yang akan dieksekusi
            
            statement.setString(1, blok7.getCatatan());
            statement.setString(2, blok7.getNKS());
            
            statement.executeUpdate();
            connection.commit();
        } catch (SQLException ex) {
            try {
                connection.rollback();
            } catch (SQLException ex1) {
                Logger.getLogger(Blok7DaoImpl.class.getName()).log(Level.SEVERE, null, ex1);
            }
            Logger.getLogger(Blok7DaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                connection.setAutoCommit(true);
            } catch (SQLException ex) {
                Logger.getLogger(Blok7DaoImpl.class.getName()).log(Level.SEVERE, null, ex);
            }
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(Blok7DaoImpl.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    @Override
    public Blok7 getBlok7ByNKS(String nks) throws EntityException {
        PreparedStatement statement = null;
        try {
            connection = DatabaseUtility.getConnection();
            connection.setAutoCommit(false);
            statement = connection.prepareStatement(getBlok7ByNKS); //isikan statement yang akan dieksekusi

            statement.setString(1, nks);
            
            ResultSet resultSet = statement.executeQuery();

            Blok7 blok7 = null;
            if (resultSet.next()) {
                blok7 = new Blok7();
                blok7.setNKS(resultSet.getString("NKS"));
                blok7.setCatatan(resultSet.getString("catatan"));
                
            } else {
                throw new SQLException("field tidak ditemukan");
            }

            connection.commit();

            return blok7;
            
        } catch (SQLException ex) {
            try {
                connection.rollback();
            } catch (SQLException ex1) {
                Logger.getLogger(Blok7DaoImpl.class.getName()).log(Level.SEVERE, null, ex1);
            }
            Logger.getLogger(Blok7DaoImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw new EntityException(ex.getMessage());
        } finally {
            try {
                connection.setAutoCommit(true);
            } catch (SQLException ex) {
                Logger.getLogger(Blok7DaoImpl.class.getName()).log(Level.SEVERE, null, ex);
            }
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(Blok7DaoImpl.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
    
}
